package com.example.util;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.toolkitDTO;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class HttpUtils {


    private String icdHost ="https://bipuat.digitalvolvo.com/bi-city-owner-service/toolkit/page";


    /**
     * post请求服务
     * @param
     * @param
     * @return
     */
    public String postFor(toolkitDTO toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        // 将JSON字符串解析为JSONObject对象
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        // 构造URL查询参数
        StringBuilder queryParams = new StringBuilder();
        for (String key : jsonObject.keySet()) {
            String value = jsonObject.getString(key);
            if (queryParams.length() > 0) {
                queryParams.append("&");
            }
            try {
                queryParams.append(URLEncoder.encode(key, "UTF-8"))
                        .append("=")
                        .append(URLEncoder.encode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        try {
            if(StringUtils.isBlank(icdHost)){
                log.error("调用接口路径为空");
            }else{
                Url = icdHost;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.get(Url+"?"+queryParams.toString());
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }


    private String getSalasOverviewStoreSummary ="https://bipuat.digitalvolvo.com/reportbi/salasDashBoardStat/getSalasOverviewStoreSummary";
    private String getSalasManageStatData ="https://bipuat.digitalvolvo.com/reportbi/salasManageStat/getSalasManageStatData";
    private String getSalasOverview ="https://bi.digitalvolvo.com/reportbi/salasDashBoardStat/getSalasOverview";
    private String getSalasContrast ="https://bipuat.digitalvolvo.com/reportbi/salasDashBoardStat/getSalasContrast";
    private String getTaendContrast ="https://bi.digitalvolvo.com/reportbi/salasDashBoardStat/getTaendContrast";

    public String postForReportbi1(Object toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        try {
            if(StringUtils.isBlank(getSalasOverviewStoreSummary)){
                log.error("调用接口路径为空");
            }else{
                Url = getSalasOverviewStoreSummary;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.post(Url,jsonString);
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }

    public String postForReportbi2(Object toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        try {
            if(StringUtils.isBlank(getSalasManageStatData)){
                log.error("调用接口路径为空");
            }else{
                Url = getSalasManageStatData;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.post(Url,jsonString);
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }

    public String postForReportbi3(Object toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        try {
            if(StringUtils.isBlank(getSalasOverview)){
                log.error("调用接口路径为空");
            }else{
                Url = getSalasOverview;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.post(Url,jsonString);
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }

    public String postForReportbi4(Object toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        try {
            if(StringUtils.isBlank(getSalasContrast)){
                log.error("调用接口路径为空");
            }else{
                Url = getSalasContrast;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.post(Url,jsonString);
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }


    public String postForReportbi5(Object toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        try {
            if(StringUtils.isBlank(getTaendContrast)){
                log.error("调用接口路径为空");
            }else{
                Url = getTaendContrast;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.post(Url,jsonString);
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }

    private String getDisplayBoard ="http://10.37.26.211/nbm/api/biApi/getDisplayBoard";
    private String getNewCarBusiness ="http://10.37.26.211/nbm/api/biApi/getNewCarBusiness";

    public String postForReportbi6(Object toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        try {
            if(StringUtils.isBlank(getDisplayBoard)){
                log.error("调用接口路径为空");
            }else{
                Url = getDisplayBoard;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.post(Url,jsonString);
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }


    public String postForReportbi7(Object toolkitDTO){
        log.info("调用接口路径:{},请求入参:{}", "/toolkit/post", JSON.toJSONString(toolkitDTO));
        String response = "";
        String Url ="";

        // 给定的JSON字符串
        String jsonString =  JSON.toJSONString(toolkitDTO);

        try {
            if(StringUtils.isBlank(getNewCarBusiness)){
                log.error("调用接口路径为空");
            }else{
                Url = getNewCarBusiness;
            }

            // 调用HttpUtil.post方法
            response = HttpUtil.post(Url,jsonString);
            log.info("请求服务成功:{},",response);
            log.info("请求服务 接口:{},成功，返回结果:{}",Url,JSON.toJSONString(response));
        } catch (Exception e) {
            log.error("请求接口:{} 失败，失败原因:{}",Url,JSON.toJSONString(e));
        }
        return response;
    }
}
